package mysql

import (
	"6-5zk/common/nacos"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	err error
	DB  *gorm.DB
)

func MysqlInit() {
	Data := nacos.Config.Mysql
	fmt.Println(Data)
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", Data.User, Data.Password, Data.Host, Data.Port, Data.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println(":mysql连接失败")
	} else {
		fmt.Println("mysql连接成功")
	}
	DB.AutoMigrate(&TakeFood{}, &Notice{})
}
